home *** CD-ROM | disk | FTP | other *** search
Lisp/Scheme | 1985-10-21 | 10.3 KB | 752 lines | [TEXT/MACA] |
- (alloc 100)
-
- (expand 24)
-
-
-
-
-
- (setq hypotheses
-
- '((animal is albatross)
-
- (animal is penguin)
-
- (animal is ostrich)
-
- (animal is eagle)
-
- (animal is apteryx)
-
- (animal is zebra)
-
- (animal is giraffe)
-
- (animal is tiger)
-
- (animal is cheetah)
-
- (animal is squirrel)
-
- (animal is chipmunk)
-
- (animal is otter)))
-
-
-
-
-
-
-
- (setq baserules
-
- '((rule identify1
-
- (if (animal has hair))
-
- (then (animal is mammal)))
-
- (rule identify2
-
- (if (animal gives milk))
-
- (then (animal is mammal)))
-
- (rule identify3
-
- (if (animal has feathers))
-
- (then (animal is bird)))
-
- (rule identify4
-
- (if (animal lays eggs))
-
- (then (animal is bird)))
-
- (rule identify5
-
- (if (animal eats meat))
-
- (then (animal is carnivore)))
-
- (rule identify6
-
- (if (animal has pointed teeth)
-
- (animal has claws)
-
- (animal has forward eyes))
-
- (then (animal is carnivore)))
-
- (rule identify7
-
- (if (animal is mammal)
-
- (animal has hooves))
-
- (then (animal is ungulate)))
-
- (rule identify8
-
- (if (animal is mammal)
-
- (animal chews cud))
-
- (then (animal is ungulate)
-
- (even toed)))
-
- (rule identify9
-
- (if (animal is mammal)
-
- (animal is carnivore)
-
- (animal has tawny color)
-
- (animal has dark spots))
-
- (then (animal is cheetah)))
-
- (rule identify10
-
- (if (animal is mammal)
-
- (animal is carnivore)
-
- (animal has tawny color)
-
- (animal has black stripes))
-
- (then (animal is tiger)))
-
- (rule identify11
-
- (if (animal is ungulate)
-
- (animal has long neck)
-
- (animal has long legs)
-
- (animal has dark spots))
-
- (then (animal is giraffe)))
-
- (rule identify12
-
- (if (animal is ungulate)
-
- (animal has black stripes))
-
- (then (animal is zebra)))
-
- (rule identify13
-
- (if (animal is bird)
-
- (animal does not fly)
-
- (animal has long neck)
-
- (animal has long legs)
-
- (animal is black and white))
-
- (then (animal is ostrich)))
-
- (rule identify14
-
- (if (animal is bird)
-
- (animal does not fly)
-
- (animal swims)
-
- (animal is black and white))
-
- (then (animal is penguin)))
-
- (rule identify15
-
- (if (animal is bird)
-
- (animal does not fly)
-
- (animal has hair))
-
- (then (animal is apteryx)))
-
- (rule identify19
-
- (if (animal is bird)
-
- (animal has long wings)
-
- (animal flies at sea))
-
- (then (animal is albatross)))
-
- (rule identify20
-
- (if (animal is bird)
-
- (animal has long wings)
-
- (animal flies over land))
-
- (then (animal is eagle)))
-
- (rule identify26
-
- (if (animal is rodent)
-
- (animal collects nuts)
-
- (animal has striped tail))
-
- (then (animal is chipmunk)))
-
- (rule identify27
-
- (if (animal is rodent)
-
- (animal collects nuts)
-
- (animal has bushy tail))
-
- (then (animal is squirrel)))
-
- (rule identify28
-
- (if (animal is mammal)
-
- (animal is small)
-
- (animal has short legs))
-
- (then (animal is rodent)))
-
- (rule identify32
-
- (if (animal is mammal)
-
- (animal is carnivore)
-
- (animal swims)
-
- (animal is slender)
-
- (animal has brown fur))
-
- (then (animal is otter)))))
-
-
-
-
-
-
-
-
-
- (defun member (item s)
-
- (cond ((null s) nil)
-
- ((equal item (car s)) s)
-
- (t (member item (cdr s)))))
-
-
-
- (defun remember (newfact truth reason)
-
- (cond ((member newfact facts) nil)
-
- (t (setq facts (cons newfact facts))
-
- (setq reasoning (cons (list truth reason) reasoning))
-
- newfact)))
-
-
-
-
-
- (defun recall (fact)
-
- (cond ((member fact facts) fact)
-
- (t nil)))
-
-
-
-
-
- (defun testif (rule ifs)
-
- (setq ifs (car (cdr (cdr rule))))
-
- (while (&& (setq ifs (cdr ifs)) (recall (car ifs))))
-
- (null ifs))
-
-
-
-
-
- (defun sayso (rule then)
-
- (princ "\nRule ")
-
- (print (car (cdr rule)))
-
- (princ " deduces")
-
- (prinlist then)
-
- (princ "\n\n")
-
- (setq success t))
-
-
-
-
-
- (defun usethen (rule)
-
- (setq success nil)
-
- (setq thens (car (cdr (cdr (cdr rule)))))
-
- (while (setq thens (cdr thens))
-
- (cond ((remember (car thens) t (car (cdr rule)))
-
- (sayso rule (car thens)))))
-
- success)
-
-
-
- (defun tryrule (rule)
-
- (&& (testif rule nil) (usethen rule)))
-
-
-
-
-
-
-
- (defun testif+ (rule ifs)
-
- (setq ifs (car (cdr (cdr rule))))
-
- (while (&& (setq ifs (cdr ifs)) (verify (car ifs) nil)))
-
- (null ifs))
-
-
-
-
-
-
-
- (defun tryrule+ (rule)
-
- (&& (testif+ rule nil) (usethen rule)))
-
-
-
-
-
-
-
- (defun verify (fact relevant)
-
- (cond ((recall fact) (car (findwhy fact)))
-
- (t (setq relevant (inthen fact nil))
-
- (cond ((null relevant) (tryask fact))
-
- (t (trydeduce relevant))))))
-
-
-
-
-
- (defun ask (fact)
-
- (princ "Would you say that the")
-
- (prinlist fact)
-
- (princ "? ")
-
- (getanswer))
-
-
-
-
-
- (defun tryask (fact)
-
- (setq answer (ask fact))
-
- (remember fact answer 'saidso)
-
- answer)
-
-
-
-
-
- (defun dirdeduce (relrules)
-
- (while (&& relrules
-
- (! (tryrule (car relrules))))
-
- (setq relrules (cdr relrules)))
-
- relrules)
-
-
-
- (defun inddeduce (relrules)
-
- (while (&& relrules
-
- (! (tryrule+ (car relrules))))
-
- (setq relrules (cdr relrules)))
-
- relrules)
-
-
-
-
-
- (defun trydeduce (trelrules)
-
- (cond ((dirdeduce trelrules) t)
-
- ((inddeduce trelrules) t)
-
- (t (cond (verbose
-
- (princ "Assuming")
-
- (prinlist fact)
-
- (princ " to be untrue since it's unsupported.\n")))
-
- (remember fact nil 'exhausted) nil)))
-
-
-
- (defun thenp (fact rule)
-
- (member fact (car (cdr (cdr (cdr rule))))))
-
-
-
-
-
- (defun inthen (fact relrules)
-
- (foreach rule baserules
-
- (cond ((thenp fact rule) (setq relrules (cons rule relrules)))))
-
- relrules)
-
-
-
-
-
-
-
-
-
- (setq Copyright-April-1985-Clive-Steward t)
-
-
-
- (defun docase ()
-
- (setq possibilities hypotheses)
-
- (setq facts nil)
-
- (setq reasoning nil)
-
- (setq running nil)
-
- (princ "\n\nAnimal expert at your service...\n\nWould you like ")
-
- (princ "to be informed of progress as we work towards a result?")
-
- (setq verbose (getanswer))
-
- (princ "\n")
-
- (setq running t)
-
- (while (&& (lookpossible)
-
- (! (verify (car possibilities) nil)))
-
- (setq possibilities (cdr possibilities)))
-
- (setq running nil)
-
- (cond ((null possibilities) (princ "\nNo hypothesis confirmed...\n\n"))
-
- (t (princ "\n\nWhat you've told me indicates that the")
-
- (prinlist (car possibilities))
-
- (princ ".\n\n")
-
- (princ "Would you like an explanation? ")
-
- (cond ((getanswer) (explain)))))
-
- (cond (verbose (mem))))
-
-
-
-
-
- (defun lookpossible ()
-
- (cond ((&& verbose possibilities)
-
- (princ "Looking at possibility that")
-
- (prinlist (car possibilities))
-
- (princ ".\n")))
-
- possibilities)
-
-
-
- (defun prinlist (plist)
-
- (foreach x plist (princ " ") (princ x)))
-
-
-
- (defun findwhy (fact)
-
- (setq findex 1)
-
- (while (&& (setq curfact (nth findex facts))
-
- (! (equal fact curfact)))
-
- (setq findex (+ 1 findex)))
-
- (setq reason (nth findex reasoning)))
-
-
-
- (defun nl () (princ "\n"))
-
-
-
-
-
- (defun tab () (princ " "))
-
-
-
-
-
- (defun formatparts (element)
-
- (tab) (princ (car element)) (nl)
-
- (while (setq element (cdr element))
-
- (tab)(tab) (prinlist (car element))
-
- (nl)))
-
-
-
-
-
-
- (defun formatrule (therule)
-
- (prinlist (list (car therule) (nth 2 therule))) (princ ":")
-
- (nl) (nl)
-
- (formatparts (nth 3 therule))
-
- (formatparts (nth 4 therule)))
-
-
-
-
-
- (defun findrule (rule)
-
- (setq frules baserules)
-
- (while (&& frules (! (equal rule (car (cdr (car frules))))))
-
- (setq frules (cdr frules)))
-
- (cond (frules (car frules))
-
- (t (princ "Error: no rule named ")
-
- (princ rule)
-
- (princ "!\n"))))
-
-
-
-
-
-
-
-
-
- (defun explain ()
-
- (setq wantmore t)
-
- (foreach fact facts
-
- (cond (wantmore
-
- (princ "\n")
-
- (prinlist fact)
-
- (setq reason (findwhy fact))
-
- (princ " is")
-
- (cond ((car reason) (princ " true "))
-
- (t (princ " false ")))
-
- (cond ((equal (car (cdr reason)) 'saidso)
-
- (princ "because you said so."))
-
- ((equal (car (cdr reason)) 'exhausted)
-
- (princ "because all rules which might prove it failed."))
-
- (t (princ "because")
-
- (formatrule (findrule (car (cdr reason))))))
-
- (setq running nil)
-
- (princ "\n\nDo you want further explanation? ")
-
- (setq wantmore (getanswer))
-
- (princ "\n")))))
-
-
-
-
-
- (defun getanswer ()
-
- (setq answ (read))
-
- (cond ((member answ legalanswers) (eval (list answ)))
-
- (t (princ "\nSorry, legal answers are ")
-
- (princ ": ")
-
- (prinlist legalanswers)
-
- (princ ". Please respond again --")
-
- (getanswer))))
-
-
-
-
-
- (setq legalanswers '(yes no why))
-
-
-
-
-
- (defun no () nil)
-
-
-
-
-
- (defun yes () t)
-
-
-
-
-
-
- (defun why ()
-
- (cond (running
-
- (princ "\nBecause I'm trying to establish")
-
- (formatrule rule)
-
- (princ "\n\n")
-
- (ask fact))
-
- (t (princ "\nSorry, why is useful only when I've ")
-
- (princ "asked you for a fact....\n\n")
-
- (princ "Please answer again --")
-
- (getanswer))))
-
-
-
-
-
- (defun consult ()
-
- (setq runcase t)
-
- (while runcase
-
- (docase)
-
- (princ "\n\nWould you like to try another case? ")
-
- (setq runcase (getanswer))))
-
-
-
-
-
-
- (consult)
-
-
-
-